# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Tim Stahlhut <stahta01@gmail.com>

_realname=pupnp
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.14.12
pkgrel=1
pkgdesc="Portable SDK for UPnP* Devices (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://github.com/pupnp/pupnp"
license=("BSD")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc" "${MINGW_PACKAGE_PREFIX}-cmake" "${MINGW_PACKAGE_PREFIX}-ninja")
options=('strip' 'staticlibs')
source=(libupnp-${pkgver}.tar.bz2::https://github.com/pupnp/pupnp/releases/download/release-${pkgver}/libupnp-${pkgver}.tar.bz2
        002-libupnp-win32.patch
        010-msvc.patch
        020-fix-install-dir.patch)
sha256sums=('091c80aada1e939c2294245c122be2f5e337cc932af7f7d40504751680b5b5ac'
            '99a0334ff2dd01fdcbe37d7cfbbbc86dacbf2cb7cccd533be3c8833e4367af1c'
            '2044dda985953e5b923bd695ea73bb5ce351c5244acbc60bbdd7357baabdb1b8'
            'd62b880d3ff93c5ad549b1d724c014608ba914f0f18d750e12e22bf68d51362b')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/libupnp-${pkgver}"
  apply_patch_with_msg \
    002-libupnp-win32.patch \
    010-msvc.patch \
    020-fix-install-dir.patch
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -Dclient=ON \
    -Ddevice=ON \
    -Dwebserver=ON \
    -Dssdp=ON \
    -Doptssdp=ON \
    -Dsoap=ON \
    -Dgena=ON \
    -Dtools=ON \
    -Dipv6=ON \
    -Dunspecified_server=OFF \
    -Dopen_ssl=OFF \
    -Dblocking_tcp_connections=OFF \
    -Dscriptsupport=ON \
    -Dpostwrite=OFF \
    -Dreuseaddr=ON \
    -Dsamples=ON \
    ../libupnp-${pkgver}

  cmake --build .
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  ctest . || true
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR=${pkgdir} cmake --install .

  # License
  install -Dm644 ${srcdir}/libupnp-${pkgver}/COPYING ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
}
